package com.tjsinfo.tjsoa.baobiao.bo;

import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.baobiao.bo.IHqGjjManage;
import com.tjsinfo.tjsoa.baobiao.vo.THqZfgjjybb;
import com.tjsinfo.tjsoa.system.dao.ISystemSpringDao;


/**
 * 用户BO
 * @author 天津市电子计算机研究所
 *
 */
	
public class THqGjjManage  implements IHqGjjManage{
	private IQbcDao qbcDao;
	private IHibernateDao hibernateDao;
	private ISystemSpringDao springDao;
	
	public ISystemSpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISystemSpringDao springDao) {
		this.springDao = springDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	
	/**
	 * 根据月份、单位获得统计表信息
	 * @param yue 年份季度
	 * @param czy 操作员
	 * @return
	 */
	public List<THqZfgjjybb> findByYueYh(String yue, Integer yhid){
  
		DetachedCriteria criteria = DetachedCriteria.forClass(THqZfgjjybb.class); 
		criteria.add(Restrictions.eq("yue", yue));
		criteria.add(Restrictions.eq("yhid", yhid));
		return  (List<THqZfgjjybb>) qbcDao.getCriteria(criteria);
	}
	
	/**
	 * 追加报表
	 * @param jshtjb
	 */
	public void addBaobiao(THqZfgjjybb gjjybb){
		hibernateDao.save(gjjybb);
	}

	/**
	 * 更新报表
	 * @param jshtjb
	 */
	public void updateBaobiao(THqZfgjjybb gjjybb){
		hibernateDao.update(gjjybb);
	}
	
	/**
	 * 查找已存在的报表
	 * @param id 用户id
	 * @return List<THqZfgjjybb>
	 */
	public List<THqZfgjjybb> findByYh(Integer yhid) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(THqZfgjjybb.class); 
		criteria.add(Restrictions.eq("yhid", yhid));
		return  (List<THqZfgjjybb>) qbcDao.getCriteria(criteria);
	}
	/**
	 * 根据月份查找已提交的报表
	 * @param yueString
	 */
	public List<THqZfgjjybb> findByYue(String yueString) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(THqZfgjjybb.class); 
		criteria.add(Restrictions.eq("yue", yueString));
		criteria.add(Restrictions.eq("state", "1"));
		return  (List<THqZfgjjybb>) qbcDao.getCriteria(criteria);
	}

	/**
	 * 查询存在已提交报表的季度
	 */
	public List<THqZfgjjybb> findByState() {
		// TODO Auto-generated method stub
		
		return null;
	}

	/**
	 * 根据年份季度、单位、是否已提交获得统计表信息
	 * @param yue 年份季度
	 * @param czy 操作员
	 * @return
	 */
	public List<THqZfgjjybb> findByYueYh(String yue, Integer yhid,
			String state) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(THqZfgjjybb.class); 
		criteria.add(Restrictions.eq("yue", yue));
		criteria.add(Restrictions.eq("yhid", yhid));
		criteria.add(Restrictions.eq("state", state));		
		return  (List<THqZfgjjybb>) qbcDao.getCriteria(criteria);
	}

	/**
	 * 根据id查找统计表
	 * @param tjbid1
	 * @return
	 */
	public THqZfgjjybb findById(Integer id) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(THqZfgjjybb.class); 
		criteria.add(Restrictions.eq("id", id));
		return (THqZfgjjybb)qbcDao.getCriteria(criteria).get(0);
	}

	/**
	 * 生成包含上报统计表的季度列表
	 */
	public List<Map<String, String>> getTjJidu() {
		// TODO Auto-generated method stub
		List<Map<String, String>> tempList = springDao.commonSelect(new String[]{"yue"}, "SELECT yue FROM t_hq_jshbfkhtjb WHERE state = '1' GROUP BY yue ORDER BY yue DESC");
		return tempList;
	}
	
	/**
	 * 汇总统计数据
	 * @param yueString
	 * @param string
	 * @return
	 */
	public THqZfgjjybb collectByYue(String yueString) {
		// TODO Auto-generated method stub
		
		String[] fieldsStrings = new String[]{"yue","s_zcrsh","s_byfcrsh","s_ljfcrsh","s_txzhq_rsh","s_txzhq_je","s_gfzhq_rsh","s_gfzhq_je","s_hdzhq_rsh","s_hdzhq_je","s_qtzhq_rsh","s_qtzhq_je","s_byxkhrsh","s_yje","s_bydrrsh","s_byzhrje","s_bydchrsh","s_byzhchje","s_byhjrsh","s_yhjje","s_gjjjcye"};
		StringBuffer SQlBuffer = new StringBuffer(); 
			SQlBuffer.append(" SELECT yue, sum(zcrsh)as s_zcrsh, sum(byfcrsh)as s_byfcrsh, sum(ljfcrsh)as s_ljfcrsh, sum(txzhq_rsh)as s_txzhq_rsh, sum(txzhq_je)as s_txzhq_je,"); 
			SQlBuffer.append(" sum(gfzhq_rsh)as s_gfzhq_rsh, sum(gfzhq_je)as s_gfzhq_je, sum(hdzhq_rsh)as s_hdzhq_rsh, sum(hdzhq_je)as s_hdzhq_je,"); 
			SQlBuffer.append(" sum(qtzhq_rsh)as s_qtzhq_rsh, sum(qtzhq_je)as s_qtzhq_je, sum(byxkhrsh)as s_byxkhrsh, sum(yje)as s_yje, sum(bydrrsh)as s_bydrrsh, sum(byzhrje)as s_byzhrje,"); 
			SQlBuffer.append(" sum(bydchrsh)as s_bydchrsh, sum(byzhchje)as s_byzhchje, sum(byhjrsh)as s_byhjrsh, sum(yhjje)as s_yhjje,"); 
			SQlBuffer.append(" sum(gjjjcye)as s_gjjjcye FROM t_hq_zfgjjybb WHERE state = '1' AND yue = '");
			SQlBuffer.append(yueString);
			SQlBuffer.append("' GROUP BY yue");
		Map<String, String> map = springDao.commonSelect(fieldsStrings, SQlBuffer.toString()).get(0);
		THqZfgjjybb tjbtemp = new THqZfgjjybb(map.get("yue"),map.get("s_zcrsh"),map.get("s_byfcrsh"),map.get("s_ljfcrsh"),map.get("s_txzhq_rsh"),map.get("s_txzhq_je"),map.get("s_gfzhq_rsh"),map.get("s_gfzhq_je"),map.get("s_hdzhq_rsh"),map.get("s_hdzhq_je"),map.get("s_qtzhq_rsh"),map.get("s_qtzhq_je"),map.get("s_byxkhrsh"),map.get("s_yje"),map.get("s_bydrrsh"),map.get("s_byzhrje"),map.get("s_bydchrsh"),map.get("s_byzhchje"),map.get("s_byhjrsh"),map.get("s_yhjje"),map.get("s_gjjjcye"));
		return tjbtemp;
	}

	/**
	 * 按时间段统计上报及时率
	 * @param startmonth
	 * @param endmonth
	 * @return
	 */
	public List<Map<String, String>> countIntime(String beginmonth,
			String endmonth) {
		// TODO Auto-generated method stub
		String[] fieldsStrings = new String[]{"yhid","intimecount"};
		String sQLString = "SELECT  yhid, COUNT(*) AS intimecount FROM  t_hq_zfgjjybb  WHERE yue >='"+beginmonth + "'AND yue <='"+ endmonth +"' AND intime = '1' GROUP BY yhid";
		List<Map<String, String>> maplist = springDao.commonSelect(fieldsStrings, sQLString);
		return maplist;
	}

	/**
	 * 按年度统计上报及时率
	 * @param startmonth
	 * @param endmonth
	 * @return
	 */
	public List<Map<String, String>> countIntime(String yearString) {
		// TODO Auto-generated method stub
		
		String[] fieldsStrings = new String[]{"yhid","intimecount"};
		String sQLString = "SELECT  yhid, COUNT(*) AS intimecount FROM  t_hq_zfgjjybb  WHERE yue LIKE '"+yearString + "%' AND intime = '1' GROUP BY yhid";
		List<Map<String, String>> maplist = springDao.commonSelect(fieldsStrings, sQLString);
		return maplist;
	}

}
